Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rds): Correct ARN in IAM policy for IAM database access #25141

Merged
merged 3 commits into from
Apr 20, 2023
Merged

fix(rds): Correct ARN in IAM policy for IAM database access #25141

merged 3 commits into from
Apr 20, 2023

Conversation

akash1810
Copy link
Contributor

@akash1810 akash1810 commented Apr 15, 2023

The IAM policy for IAM database access takes the form of:

arn:aws:rds-db:region:account-id:dbuser:DbiResourceId/db-user-name

Following aws-cloudformation/cloudformation-coverage-roadmap#105, this change updates the ARN used in grantConnect to this format.

Additionally, update the signature of grantConnect to take an optional dbUser, which is defaulted to the master username of the database, obtained via the available Secret.

This signature change also matches grantConnect in DatabaseProxy. See #12416.

Closes #11851.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team April 15, 2023 08:26
@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort p1 labels Apr 15, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@akash1810 akash1810 changed the title fix(aws-rds): Correct ARN in IAM policy for IAM database access fix(rds): Correct ARN in IAM policy for IAM database access Apr 15, 2023
@akash1810 akash1810 marked this pull request as ready for review April 15, 2023 21:34
@akash1810
Copy link
Contributor Author

Clarification Request.

@aws-cdk-automation aws-cdk-automation added the pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run label Apr 15, 2023
@@ -130,6 +147,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
public readonly instanceEndpoint = new Endpoint(attrs.instanceEndpointAddress, attrs.port);
public readonly engine = attrs.engine;
protected enableIamAuthentication = true;
public readonly instanceResourceId = attrs.instanceResourceId;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new, required property. Is it a breaking change for fromDatabaseInstanceAttributes?

Copy link
Contributor Author

@akash1810 akash1810 Apr 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new, required property. Is it a breaking change for fromDatabaseInstanceAttributes?

Ahh the AWS CodeBuild report suggests it is breaking:

err  - IFACE aws-cdk-lib.aws_rds.DatabaseInstanceAttributes: newly required property 'instanceResourceId' added: input to aws-cdk-lib.aws_rds.DatabaseInstanceBase.fromDatabaseInstanceAttributes [strengthened:aws-cdk-lib.aws_rds.DatabaseInstanceAttributes]
Some packages seem to have undergone breaking API changes. Please avoid.

Advice on how to proceed welcomed 🙏🏽.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can change this to an optional property in IDatabaseInstance and DatabaseInstanceBase and fromDatabaseInstanceAttributes.

Copy link
Contributor Author

@akash1810 akash1810 Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can change this to an optional property in IDatabaseInstance and DatabaseInstanceBase and fromDatabaseInstanceAttributes.

Done in ce630ec.

I'm not a huge fan of this as it means one has to ensure the property is not undefined when accessing it, which adds bloat IMO:

const db = new DatabaseInstance();
const resourceId: string | undefined = db.instanceResourceId;

if (!resourceId) {
  // unhappy path
}
else {
  // happy path
}

Alternatively, one can use non-null assertion, however I've worked in projects where this would produce a lint warning.

const db = new DatabaseInstance();
const resourceId: string = db.instanceResourceId!;

Additionally, this being optional makes it the only optional property1. Not a problem in general, however might be an indication of not being desired?

Footnotes

  1. enableIamAuthentication doesn't really count, as an optional boolean still has two values: true = true, false|undefined = false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's not great, but it's the only way to avoid breaking changes.

@corymhall corymhall self-assigned this Apr 17, 2023
@akash1810
Copy link
Contributor Author

akash1810 commented Apr 20, 2023

Exemption Request

Unit tests have been updated, and an additional one added.

Running the tests via yarn --cwd packages/aws-cdk-lib build+test doesn't produce any git diff, so not sure what integration and/or snapshot files need updating.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Apr 20, 2023
The IAM policy for IAM database access takes the form of:

```
arn:aws:rds-db:region:account-id:dbuser:DbiResourceId/db-user-name
```

Update the ARN used in `grantConnect` to this format.

Additionally, update the signature of `grantConnect` to take an optional `dbUser`,
which is defaulted to the master username of the database.

This signature change also matches `grantConnect` in `DatabaseProxy`.

See:
 - https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.IAMPolicy.html
 - aws-cloudformation/cloudformation-coverage-roadmap#105
To avoid a breaking change, make `instanceResourceId` an optional prop.
@corymhall corymhall added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Apr 20, 2023
@corymhall
Copy link
Contributor

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

PRs must pass status checks before we can provide a meaningful review.

Integration tests are now added in @aws-cdk-testing/framework-integ. In this case I think the integ test would only be useful if we created an assertion test where something (i.e. lambda function) was able to connect to the database, but I think the effort to create that case is too large so I'm exempting it.

Copy link
Contributor

@corymhall corymhall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@aws-cdk-automation aws-cdk-automation dismissed their stale review April 20, 2023 11:31

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@mergify
Copy link
Contributor

mergify bot commented Apr 20, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@corymhall
Copy link
Contributor

@akash1810 you need to authorize modification on the head branch in order for mergify to merge this.

@akash1810
Copy link
Contributor Author

akash1810 commented Apr 20, 2023

@akash1810 you need to authorize modification on the head branch in order for mergify to merge this.

Ah. Done that now. Thanks! Do I need to do anything to re-run mergify?

@corymhall
Copy link
Contributor

@Mergifyio requeue

@mergify
Copy link
Contributor

mergify bot commented Apr 20, 2023

requeue

✅ The queue state of this pull request has been cleaned. It can be re-embarked automatically

@gitpod-io
Copy link

gitpod-io bot commented Apr 20, 2023

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: fc0aaa2
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 227ea09 into aws:main Apr 20, 2023
@mergify
Copy link
Contributor

mergify bot commented Apr 20, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@akash1810 akash1810 deleted the fix-grantConnect branch April 20, 2023 12:58
@ChrisLahaye
Copy link

We are experiencing some issues after upgrading to v2.77.0 which I believe is due to this change.

When using databaseInstance.grantConnect(iamRole) the following error is thrown:

Error: Resolution error: Resolution error: Resolution error: Resolution error: Synthing a secret value to Resources/${Token[xxx.ServiceAccount.Role.DefaultPolicy.Resource.LogicalID.2000]}/Properties/policyDocument/Statement/0/Resource. Using a SecretValue here risks exposing your secret. Only pass SecretValues to constructs that accept a SecretValue property, or call AWS Secrets Manager directly in your runtime code. Call 'secretValue.unsafeUnwrap()' if you understand and accept the risks.

I think this happens due to the following new code:

dbUser = this.secret.secretValueFromJson('username').toString();

Perhaps this should be unsafeUnwrap instead of toString?

@nick-asher
Copy link

Don't suppose there's any appetite to populate this property when fetching a database via FindDatabases()? There's no clear way defined to actually populate this property in this commit unless I'm missing something? The one comment in the docs about it being logged in CloudTrail doesn't feel like a sensible way to fetch the Id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort p1 pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-rds): grantConnect for IAM authentication provides invalid permissions (surface DbiResourceId)
5 participants